//
//  MovieDetailViewController.m
//  Streaming
//
//  Created by 韩一博 on 15/6/26.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "MovieDetailViewController.h"

@interface MovieDetailViewController ()

@end

@implementation MovieDetailViewController

- (void)dealloc
{
    [self.tableView release];
    [self.moderArray release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"相关视频&拍摄花絮";
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
//    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    self.HUD.labelText = @"正在加载中";
//    [self.HUD show:YES];
    
    [self createTable];
    [self afnGetRequest];
}

- (void)createTable
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    [self.tableView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-unie642.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    [self.tableView release];
}
- (void)leftBarButtonItemAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.moderArray.count;

}

-(void)afnGetRequest
{
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.m.mtime.cn/Movie/Video.api?movieId=%ld",(long)self.movieID];
    NSLog(@"self.string = %@", url_string);
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//                    NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSMutableDictionary *allDic = responseObject;
        NSMutableArray *array = [NSMutableArray array];
        array = [allDic objectForKey:@"videoList"];
        NSLog(@"array = %@",array);
//        NSLog(@"array = %@", array);
        
        if (array.count == 0) {

            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"亲，对不起" message:@"该影片没有预告片" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            [alertView release];
        }else{
        self.moderArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            MovieDetail *movieDet = [[MovieDetail alloc] init];
            movieDet.movieImageView = [dic objectForKey:@"image"];
            movieDet.nameLabel = [dic objectForKey:@"title"];
            movieDet.length = [[dic objectForKey:@"length"]integerValue];
            movieDet.url = [dic objectForKey:@"url"];
            [self.moderArray addObject:movieDet];
            [movieDet release];
        }
        }
        
        [self.tableView reloadData];
     [self.HUD hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
                [self.HUD hide:YES];
    }];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    MovieDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MovieDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    MovieDetail *movieDet = [self.moderArray objectAtIndex:indexPath.row];
    cell.movieDetail = movieDet;
//    NSLog(@"cell.movie = %@", cell.movieDetail);
    return cell;
}



#warning 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    ButtonViewController *tableVC = [[ButtonViewController alloc] init];
//    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:tableVC];
    MovieDetail *movieD = [self.moderArray objectAtIndex:indexPath.row];
    tableVC.movieUrl = movieD.url;
    NSLog(@"@@@@%@", tableVC.movieUrl);
    [self.navigationController presentViewController:tableVC animated:YES completion:^{
        
    }];
    [tableVC release];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
